import { defineStore } from 'pinia';

interface UserState {
  nickname: string;
  avatar: string;
  favorites: Array<{
    id: string;
    title: string;
    content: string;
    timestamp: number;
  }>;
}

export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    nickname: '',
    avatar: '',
    favorites: []
  }),
  
  actions: {
    updateNickname(nickname: string) {
      this.nickname = nickname;
    },
    
    addFavorite(favorite: { id: string; title: string; content: string; }) {
      this.favorites.push({
        ...favorite,
        timestamp: Date.now()
      });
    },
    
    removeFavorite(id: string) {
      const index = this.favorites.findIndex(f => f.id === id);
      if (index > -1) {
        this.favorites.splice(index, 1);
      }
    },
    
    updateFavoriteTitle(id: string, title: string) {
      const favorite = this.favorites.find(f => f.id === id);
      if (favorite) {
        favorite.title = title;
      }
    }
  },
  
  persist: {
    enabled: true
  }
});